package com.lingkun.demo.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lingkun.demo.contants.Constant;
import com.lingkun.demo.sys.entity.UserRole;
import com.lingkun.demo.sys.entity.sysVo.*;
import com.lingkun.demo.sys.service.SysUserService;
import com.lingkun.demo.sys.service.UserRoleService;
import com.lingkun.demo.utils.DataResult;
import com.lingkun.demo.utils.JwtTokenUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小龙
 * @since 2021-04-24
 */
@RestController
@Api(tags = "用户管理")
@RequestMapping("/auth")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private UserRoleService userRoleService;


    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public DataResult  login(@RequestBody LoginReqVO vo){
        LoginRespVO date  =  sysUserService.login(vo);
        return DataResult.getResult(20000,"success",date)  ;
    }

    @GetMapping("/info")
    @ApiOperation(value = "获取个人信息")
    public DataResult info(@RequestHeader(Constant.ACCESS_TOKEN) String token) {
        infoVo infoVo = new infoVo();
        Claims claimsFromToken = JwtTokenUtil.getClaimsFromToken(token);
        infoVo.setName((String) claimsFromToken.get(Constant.JWT_USER_NAME));
        infoVo.setAvatar((String) claimsFromToken.get(Constant.JWT_USER_AVATAR));
        infoVo.setIntroduction((String) claimsFromToken.get(Constant.JWT_USER_INTRODUCTION));
        infoVo.setRoles((List<String>) claimsFromToken.get(Constant.ROLES_INFOS_KEY));
        return DataResult.getResult(20000,"success",infoVo);
    }

    @GetMapping("/routes")
    @ApiOperation(value = "获取路由信息")
    public DataResult getMenu(){
        List<menuOne> menuList = sysUserService.getMenus();
        return DataResult.getResult(20000,"success",menuList);
    }


    @GetMapping("/search")
    @ApiOperation(value = "获取全部用户信息")
    public DataResult searchUsers(){
        List<userVo> userList = sysUserService.findAll();
        return DataResult.getResult(20000,"seccess",userList);
    }

    @PutMapping("/update/roles/{roleId}")
    @ApiOperation(value = "给用户分配权限信息")
    public DataResult assignUserRoles(@PathVariable("roleId") String roleId, @RequestBody List<String> data){
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
          queryWrapper.eq("User_id",roleId);
          userRoleService.remove(queryWrapper);
          List<UserRole> list = new ArrayList<>();
          if(data.size()!=0) {
           for (String datum : data) {
               UserRole userRole = new UserRole();
               userRole.setUserId(roleId);
               userRole.setRoleId(datum);
               list.add(userRole); }
           userRoleService.saveBatch(list);
       }
        return DataResult.getResult(20000);
    }




}

